package com.heima.leadnews.article.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.leadnews.article.mapper.ApArticleConfigMapper;
import com.heima.leadnews.article.mapper.ApArticleContentMapper;
import com.heima.leadnews.article.mapper.ApArticleMapper;
import com.heima.leadnews.article.service.ApArticleService;
import com.heima.model.article.pojo.ApArticle;
import com.heima.model.article.pojo.ApArticleConfig;
import com.heima.model.article.pojo.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmNewsDto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper,ApArticle> implements ApArticleService {

    @Autowired
    private ApArticleMapper apArticleMapper;

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @Autowired
    private ApArticleContentMapper apArticleContentMapper;

    @Override
    public ResponseResult load(Integer size, String tag) {
        List<ApArticle> data = apArticleMapper.load(size, tag);
        return ResponseResult.okResult(data);
    }

    @Override
    public ResponseResult loadmore(Integer size, String tag, Date min) {
        List<ApArticle> data = apArticleMapper.loadmore(size, tag,min);
        return ResponseResult.okResult(data);

    }

    @Override
    public ResponseResult loadnew(Integer size, String tag, Date max) {
        List<ApArticle> data = apArticleMapper.loadnew(size, tag,max);
        return ResponseResult.okResult(data);
    }

    @Override
    public ResponseResult saveArticle(WmNewsDto wmNewsDto) {
        //1.保存ap_article表
        ApArticle apArticle = new ApArticle();
        apArticle.setTitle(wmNewsDto.getTitle());
        apArticle.setAuthorId(wmNewsDto.getUserId().longValue());
        apArticle.setAuthorName(wmNewsDto.getAuthorName());
        apArticle.setChannelId(wmNewsDto.getChannelId());
        apArticle.setChannelName(wmNewsDto.getChannelName());
        apArticle.setLayout(wmNewsDto.getType());
        apArticle.setFlag((byte) 0);
        apArticle.setImages(wmNewsDto.getImages());
        apArticle.setLabels(wmNewsDto.getLabels());
        apArticle.setLikes(0);
        apArticle.setCollection(0);
        apArticle.setComment(0);
        apArticle.setViews(0);
        apArticle.setCreatedTime(wmNewsDto.getCreatedTime());
        apArticle.setPublishTime(wmNewsDto.getPublishTime());
        save(apArticle);
        //2.保存ap_article_config表
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setArticleId(apArticle.getId());
        apArticleConfig.setIsComment(true);
        apArticleConfig.setIsForward(true);
        apArticleConfig.setIsDown(false);
        apArticleConfig.setIsDelete(false);
        apArticleConfigMapper.insert(apArticleConfig);
        //3.保存保存ap_article_content表
        ApArticleContent apArticleContent =new ApArticleContent();
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContent.setContent(wmNewsDto.getContent());
        apArticleContentMapper.insert(apArticleContent);
        return ResponseResult.okResult(apArticle.getId());
    }

    @Override
    public ResponseResult updateStaticUrl(Long id, String url) {
        LambdaUpdateWrapper<ApArticle> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(ApArticle::getId,id)
                .set(ApArticle::getStaticUrl,url);
        update(updateWrapper);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
